package cc.marcio.systems.seven.dao;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import cc.marcio.systems.seven.model.Message;


@SuppressWarnings("serial")
public class MessageDao extends BaseDaoImpl<Message> implements BaseDao<Message> {

	protected MessageDao(Session session) {
		super(session);
	}

	@Override
	protected void addPropertiedToCriteria(Criteria criteria, Message example) {
		if( example.getUser() != null ) {
			criteria.add(Expression.eq("user", example.getUser()));
		}
	}

	@Override
	protected void addOrderToCriteria(Criteria criteria) {
		criteria.addOrder(Order.asc("date"));
	}
	
	@SuppressWarnings("unchecked")
	public List<Message> listNewMessages(Message message) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR, -9);
		Criteria criteria = this.mountCriteria(message);
		criteria.add(Restrictions.gt("date", cal.getTime()));
		return criteria.list();
	}
}
